package com.ridbor001.demo.servlet;

import com.ridbor001.demo.bean.RDBUser;
import com.ridbor001.demo.service.RDBGradeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/deleteGrade")
public class RDBDeleteGradeServlet extends HttpServlet {

    RDBGradeService gradeService = new RDBGradeService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        HttpSession session = req.getSession();
        RDBUser user = (RDBUser) session.getAttribute("user");
        if(!user.isTeacher()){
            resp.sendError(HttpServletResponse.SC_FORBIDDEN,"无权限");
            return;
        }
        String gradeId = req.getParameter("gradeId");
        boolean isDeleted = false;
        try {
            isDeleted = gradeService.deleteGrade(gradeId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if(isDeleted) {
            resp.sendRedirect("/gradeManage?searchKey");
        }
        else
            resp.sendRedirect("error.jsp");
        }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }
    }

